package com.shanghaionstar.carmanage.presenter;

import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.sqlite.entities.OvdHistory;
import com.shanghaionstar.carmanage.iview.ICarDataRefreshView;
import com.shanghaionstar.home.model.DataRefreshParser;
import com.shanghaionstar.home.model.HomeModel;
import com.shanghaionstar.sqlite.DataRefreshDBService;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.DateFormatUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by 6335yz on 2016/7/24.
 */
public class CarDataRefreshPresenter extends BasePresenter {

    private static final float START_DEGREE = 0f;
    private static final float END_DEGREE = 180f;
    private ICarDataRefreshView carDataRefreshView;
    private BaseActivity baseActivity;
    private DataRefreshDBService dataRefreshDBService;
    private float petrol_pointer_id_degressQuondam=0;

    public CarDataRefreshPresenter(IMVPScheduler mvpScheduler, BaseActivity baseActivity) {
        super(mvpScheduler);
        this.baseActivity = baseActivity;
        dataRefreshDBService = new DataRefreshDBService(baseActivity);

    }

    /**
     * 判断车辆属于电动，油电混合，纯油，是否胎压
     */
    public void initPageShow() {
        if (ApplicationUtil.getInstance().isPhev()) {
            if (carDataRefreshView != null) {
                carDataRefreshView.showPhevView(true);
            }
        }
        if (isTireSupport()) {
            if (carDataRefreshView != null) {
                carDataRefreshView.showTireView(true);
            }
        }
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        carDataRefreshView = (ICarDataRefreshView) view;
    }

    /**
     * 此处看看能否复用homefragment的homemodel
     *
     * @param homeModel 车辆数据封装类
     */
    public void getHistoryOVD() {
        String vin = ApplicationUtil.getInstance().getVin();
        OvdHistory ovdhistory = dataRefreshDBService.getOvdHistory(ApplicationUtil.getInstance().getVin());

        if (carDataRefreshView != null&& ovdhistory != null) {
            HomeModel homeModel = new DataRefreshParser().dataRefreshHomeModel(ovdhistory.getContent(), DateFormatUtil.format3(baseActivity, ovdhistory.getCreateDate(), 8));
            //carDataRefreshView.onReponseSuccess(loadRateStatus(homeModel));
            carDataRefreshView.showLoadData(loadRateStatus(homeModel));
        }

    }



    private HomeModel loadRateStatus(HomeModel homeModel) {
        if (homeModel == null) return null;
        List<String> tireList = new ArrayList<String>();
        List<String> statusList = new ArrayList<String>();
        tireList.add(homeModel.leftFront.toLowerCase(Locale.CHINA));
        tireList.add(homeModel.leftRear.toLowerCase(Locale.CHINA));
        tireList.add(homeModel.rightFront.toLowerCase(Locale.CHINA));
        tireList.add(homeModel.rightRear.toLowerCase(Locale.CHINA));

        // 胎压
        if (!CommonUtil.isNull(homeModel.leftFront) && !CommonUtil.isNull(homeModel.leftRear) && !CommonUtil.isNull(homeModel.rightFront)
                && !CommonUtil.isNull(homeModel.rightRear)) {
            if (!tireList.contains("yellow") && !tireList.contains("red")) {
                statusList.add("green");
            } else if (tireList.contains("red")) {
                statusList.add("red");
            } else {
                statusList.add("yellow");
            }
        }

        // 机油
        if (homeModel.oilLife != -1) {//-1代表该车不支持机油检查
            if (homeModel.oilLife > 20 && homeModel.oilLife <= 100) {
                statusList.add("green");
            } else if (homeModel.oilLife > 0 && homeModel.oilLife <= 20) {
                statusList.add("yellow");
            } else {
                statusList.add("red");
            }
        }
        // 燃油
        if (homeModel.fuelTankInfo > 92 && homeModel.fuelTankInfo <= 100) {
            statusList.add("green");
        } else if (homeModel.fuelTankInfo > 30 && homeModel.fuelTankInfo <= 92) {
            statusList.add("green");
        } else if (homeModel.fuelTankInfo > 8 && homeModel.fuelTankInfo <= 30) {
            statusList.add("yellow");
        } else {
            statusList.add("red");
        }
        //电量
        if (CommonUtil.isNumerice(homeModel.batteryLevel)) {
            float iBatteryLevel = Float.valueOf(homeModel.batteryLevel);
            if (iBatteryLevel > 20 && iBatteryLevel <= 100) {
                statusList.add("green");
            } else if (iBatteryLevel > 0 && iBatteryLevel <= 20) {
                statusList.add("yellow");
            } else {
                statusList.add("red");
            }
        }

        //test value animation
        homeModel.fuel_value  = 70f;
        homeModel.fuelDegree = getDegreeByValue(homeModel.fuel_value);
        return homeModel;
    }

    private float getDegreeByValue(float value) {
        //起始度数73.2f,结束度数96f; 每升汽油代表(73.2+96)/100;
        if (value < 0) {
            value = 0;
        }
        if (value > 100) {
            value = 100;
        }
        float degree = value * (END_DEGREE - START_DEGREE) / 100f;
        return (START_DEGREE + degree);
    }

    private boolean isTireSupport() {
        boolean support = false;
        String[] diagnostics = ApplicationUtil.getInstance().getSupportedDiagnostic();
        if (diagnostics != null && diagnostics.length > 0) {
            for (String diagnostic : diagnostics) {
                if (Constants.TIRE_PRESSURE.equals(diagnostic)) {
                    support = true;
                    break;
                }
            }
        }
        return support;
    }


    public RotateAnimation AniRotateImage(float degressQuondam, float fDegress, final int tag) {
        float pivotX = DeviceUtil.dip2px(baseActivity, 10f);
        float pivotY = DeviceUtil.dip2px(baseActivity, 45f);
        RotateAnimation myAni = new RotateAnimation(degressQuondam, fDegress, pivotX, pivotY);
        myAni.setDuration(500);
        myAni.setFillAfter(true);
        myAni.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }
        });
        petrol_pointer_id_degressQuondam = fDegress;
        return myAni;
    }
}
